package cn.jinjimi.app.main.widget.biz;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.base.preference.PreferenceManager;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.util.BitmapUtils;

/**
 * Created by Roy
 * Date: 16/7/28
 */
public class TipWindow extends PopupWindow {
    private Context mContext;
    private RelativeLayout rlRoot;
    private ImageView img;
    private TextView tvTips;

    // 记录提示出现的SharePerence Key
    private String key;

    public TipWindow(Context context) {
        this.mContext = context;
        View view = View.inflate(context, R.layout.window_tip, null);
        setContentView(view);

        rlRoot = (RelativeLayout) view.findViewById(R.id.rl_root);
        img = (ImageView) view.findViewById(R.id.img);
        tvTips = (TextView) view.findViewById(R.id.tv_tips);

        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(false);
        setTouchable(false);
        setFocusable(false);

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (StringUtils.isNotEmpty(key))
                    PreferenceManager.setData(key, true);
            }
        });
    }

    public TipWindow setText(int resId) {
        tvTips.setText(mContext.getString(resId));
        return this;
    }

    public TipWindow setkey(String key) {
        this.key = key;
        return this;
    }

    public TipWindow setRightMargin(int resId) {
        rlRoot.setPadding(0, 0, mContext.getResources().getDimensionPixelOffset(resId), 0);
        return this;
    }

    public TipWindow setAlpha(int alpha) {
        Bitmap bitmapUp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.tips_pic_up);

        bitmapUp = BitmapUtils.setAlpha(bitmapUp, alpha);
        img.setImageBitmap(bitmapUp);
        return this;
    }

    public TipWindow setCanReturn(boolean canReturn) {
        setFocusable(canReturn);

        if (canReturn) {
            getContentView().setFocusable(true);//设置view能够接听事件
            getContentView().setFocusableInTouchMode(true); //设置view能够接听事件
            getContentView().setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        if (this != null)
                            dismiss();
                        return true;
                    }
                    return false;
                }
            });
        }
        return this;
    }
}
